package org.lmj.dos.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.lmj.dos.common.bean.ServerResponse;
import org.lmj.dos.common.pojo.CartEntity;
import org.lmj.dos.mapper.CartMapper;
import org.lmj.dos.web.resultdo.CartsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Stream;

@RequestMapping
@RestController
public class CartsController {

    @Autowired
    CartMapper cartMapper;


    @GetMapping("/carts/products/{userId}")
    public ServerResponse cartsListSum(@PathVariable("userId") Integer userId)  {
        QueryWrapper<CartEntity> wrapper = new QueryWrapper<>();
        wrapper.in("user_id",userId);
        List<CartEntity> cartEntities = cartMapper.selectList(wrapper);
        if (!CollectionUtils.isEmpty(cartEntities)){
            return  ServerResponse.success(cartEntities.size());
        }else{
            return ServerResponse.success(0);
        }
    }


    @GetMapping("/carts/{userId}")
    public ServerResponse cartsList(@PathVariable("userId") Integer userId) throws IOException {
        QueryWrapper<CartEntity> wrapper = new QueryWrapper<>();
        wrapper.in("user_id",userId);
        List<CartEntity> cartEntities = cartMapper.selectList(wrapper);
        Double total=0.0;
        for (CartEntity cartEntity:cartEntities){
            total+=cartEntity.getProductTotalPrice();
        }
//        AtomicReference<Double> total= new AtomicReference<Double>(0.0);
//        cartEntities.stream().map(cartEntity -> total.updateAndGet(v -> v + cartEntity.getProductTotalPrice()));
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("cartTotalPrice",total);
        stringObjectHashMap.put("cartList",cartEntities);
        return ServerResponse.success(stringObjectHashMap);
    }

    @PostMapping("/carts")
    public ServerResponse<CartsVo.GetPostCarts> cartsAdd(@RequestBody CartEntity cartEntity) throws IOException {

        int insert = cartMapper.insert(cartEntity);
        return ServerResponse.success();
    }

    /**
     * 删除购物车
     */
    @PostMapping("/deleteCart")
    public void deleteCart(@RequestBody Map<String,Object> map) throws IOException {
        Integer userId = (Integer) map.get("userId");
        List<Integer> list= (List<Integer>) map.get("productIds");
        if (!CollectionUtils.isEmpty(list)){
            QueryWrapper<CartEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id",userId);
            wrapper.in("product_id",list);
            cartMapper.delete(wrapper);
        }

    }

    @PutMapping("/carts/{productId}")
    public ServerResponse<CartsVo.GetPostCarts> cartsNumAdd(@PathVariable("productId")String productId) throws IOException {
        ClassPathResource classPathResource = new ClassPathResource("post-carts.json");
        InputStream inputStream =classPathResource.getInputStream();
        ObjectMapper objectMapper = new ObjectMapper();

        CartsVo.GetPostCarts getOrders = objectMapper.readValue(inputStream, CartsVo.GetPostCarts.class);
        return ServerResponse.success(getOrders);
    }
    @DeleteMapping("/carts/{productId}")
    public ServerResponse<CartsVo.GetPostCarts> cartsNumDelete(@PathVariable("productId")String productId) throws IOException {
        ClassPathResource classPathResource = new ClassPathResource("post-carts.json");
        InputStream inputStream =classPathResource.getInputStream();
        ObjectMapper objectMapper = new ObjectMapper();

        CartsVo.GetPostCarts getOrders = objectMapper.readValue(inputStream, CartsVo.GetPostCarts.class);
        return ServerResponse.success(getOrders);
    }
}
